// index.js
// 获取应用实例
const app = getApp()
Page({
  data: {
    item: 0, //页面id
    playState: '1', //默认为1，1为播放，0为暂停
    state: "paused", //设置专辑封面动画状态
    playStateImage: '/images/icon/播放.png',
    currentMusicId: 0, //当前播放音乐id
    playList: [
      {
        id: 1, title: "Stardew Valley Overture", singer: "ConcernedApe",
        src: "http://127.0.0.1:8080/Stardew Valley Overture.mp3"
      },
      {
        id: 2, title: "Mines", singer: "ConcernedApe",
        src: "http://127.0.0.1:8080/Mines.mp3"
      },
      {
        id: 3, title: "Load Game", singer: "ConcernedApe",
        src: "http://127.0.0.1:8080/Load Game.mp3"
      },
      {
        id: 4, title: "In the Deep Woods", singer: "ConcernedApe",
        src: "http://127.0.0.1:8080/In the Deep Woods.mp3"
      },
    ], //播放音乐队列
    play: {
      title: "",
      singer: "",
      playIndex: ""
    } //当前播放音乐信息
  },

  // 页面切换
  changeItem: function (e) {
    console.log(e.target.dataset.item)
    this.setData({
      item: e.target.dataset.item
    })
  },

  // 播放功能
  audioCtx: null,
  onReady: function () {
    this.audioCtx = wx.createInnerAudioContext() //获取audioCtx对象
    this.setMusic(this.data.currentMusicId) //初始化播放音乐
  },

  // 设置播放音乐src
  setMusic: function (index) {
    var music = this.data.playList[index]
    this.audioCtx.src = music.src
    this.setData({
      'play.title': music.title,
      'play.singer': music.singer,
      'play.playIndex': music.id
    })
  },

  // 播放暂停音乐
  playMusci: function () {
    if (this.data.playState == '1') {
      console.log("===播放成功===")
      this.audioCtx.play()
      this.setData({
        state: 'running',
        playState: '0',
        playStateImage: '/images/icon/暂停.png'
      })
    } else {
      console.log("===暂停成功===")
      this.audioCtx.pause()
      this.setData({
        state: 'paused',
        playState: '1',
        playStateImage: '/images/icon/播放.png'
      })
    }
  },

  // 下一首歌曲
  nextMusic: function () {
    if (this.data.playState == '1') {
      this.whetherLastSong() //判断是否到播放列表最后有一曲
      this.setData({
        state: 'running',
        playState: '0',
        playStateImage: '/images/icon/暂停.png'
      })
    } else {
      this.whetherLastSong()
    }
  },

  // 是否到播放队列最后
  whetherLastSong: function () {
    console.log("===下一曲===")
    var index = this.data.currentMusicId + 1
    var lenth = this.data.playList.length
    if (index >= lenth) {
      this.setMusic(0)
      this.audioCtx.play()
      this.data.currentMusicId = 0
    } else {
      this.setMusic(index)
      this.audioCtx.play()
      this.data.currentMusicId = index
    }
  },

  // 列表播放
  change: function (e) {
    console.log(e.currentTarget.dataset.index)
    var index = e.currentTarget.dataset.index
    this.setMusic(index)
    this.audioCtx.play()
    this.setData({
      state: 'running',
      playState: '0',
      currentMusicId: index,
      playStateImage: '/images/icon/暂停.png'
    })
  },

  // 列表跳转
  changePage: function () {
    this.setData({
      item: 2
    })
  }

})
